package spring.boot.adminx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import spring.boot.adminx.model.WxPhoto;
import spring.boot.adminx.service.FastDFSService;
import spring.boot.adminx.service.WxPhotoService;

import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/wx/mini/admin/photo")
public class WxPhotoController {

    @Value("${fdfs.http.path}")
    public String path;

    @Autowired
    private FastDFSService fastDFSService;


    @Autowired
    private WxPhotoService wxPhotoService;

    @GetMapping("list/{wxMiniAppId}")
    public String list(@PathVariable Long wxMiniAppId,String enable, Model model){
        QueryWrapper<WxPhoto> wrapper = new QueryWrapper<>();
        wrapper.select("wx_mini_app_id","no","move_type","name","max(enable) as \"enable\"");
        wrapper.eq("wx_mini_app_id",wxMiniAppId);
        if (enable!=null&&!"99".equals(enable)){
            wrapper.having("max(enable) = {0}",enable);
        }
        wrapper.groupBy("wx_mini_app_id","no","move_type","name");
        wrapper.orderByAsc("no");
        List<WxPhoto> wxPhotoList = wxPhotoService.list(wrapper);
        model.addAttribute("list", wxPhotoList);
        model.addAttribute("wxMiniAppId", wxMiniAppId);
        if (enable==null){
            model.addAttribute("enable", null);
        }else{
            model.addAttribute("enable", Integer.parseInt(enable));
        }

        return "photo/list";
    }



    @GetMapping("list/{wxMiniAppId}/{no}")
    public String list(@PathVariable Long wxMiniAppId, @PathVariable Integer no, Model model){
        QueryWrapper<WxPhoto> wrapper = new QueryWrapper<>();
        wrapper.eq("wx_mini_app_id",wxMiniAppId);
        wrapper.eq("no",no);
        List<WxPhoto> wxPhotoList = wxPhotoService.list(wrapper);
        model.addAttribute("list", wxPhotoList);
        model.addAttribute("wxMiniAppId", wxMiniAppId);
        model.addAttribute("no", no);
        return "photo/list";
    }

    @PostMapping("enable/{wxMiniAppId}/{no}/{enable}")
    @ResponseBody
    public boolean enable(@PathVariable String wxMiniAppId, @PathVariable String no, @PathVariable String enable){
        UpdateWrapper<WxPhoto> wrapper = new UpdateWrapper<>();
        wrapper.set("enable",enable);
        wrapper.eq("wx_mini_app_id",wxMiniAppId);
        wrapper.eq("no",no);
        return wxPhotoService.update(wrapper);
    }





















    @PostMapping("delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable Long id){
        WxPhoto wxPhoto = wxPhotoService.getById(id);
        String url = wxPhoto.getUrl().substring(21);
        fastDFSService.delete(url);
        return wxPhotoService.removeById(id);
    }




    @PostMapping("insert")
    public String insert(MultipartFile file, @RequestParam Long wxMiniAppId, @RequestParam Integer no, @RequestParam Integer type, @RequestParam String name){
        if (file==null){
            log.error("上传文件不能为空！");
            return null;
        }
        String filename = fastDFSService.uploadFile(file);
        WxPhoto wxPhoto = new WxPhoto();
        wxPhoto.setId(IdWorker.getId());
        wxPhoto.setWxMiniAppId(wxMiniAppId);
        wxPhoto.setName(name);
        wxPhoto.setNo(no);
        wxPhoto.setUrl(path+filename);
        wxPhoto.setCreateTime(new Date());
        wxPhoto.setType(type);
        wxPhoto.setEnable(0);
        wxPhotoService.save(wxPhoto);

        return "redirect:/wx/admin/photo/list/"+wxMiniAppId+"/"+no;
    }

}
